package com.ocom.payconfig.controller;

import com.ocom.common.constant.PayConfigUrl;
import com.ocom.common.entity.card.EntityXfAccountType;
import com.ocom.common.enums.ResultEnum;
import com.ocom.common.resp.Result;
import com.ocom.common.utils.ResultUtil;
import com.ocom.payconfig.service.XfAccountTypeService;
import com.ocom.redis.util.RedisUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@RestController
public class AccountTypeController {
    @Autowired
    private XfAccountTypeService xfAccountTypeService;

    @Resource
    private RedisUtil redisUtil;

    // 获取账号类型信息(getXFAccountType)
    @GetMapping(PayConfigUrl.Url_GetXFAccountType)
    public Result GetXFAccountType(Long comId, @RequestParam(required = false ,defaultValue = "0")Integer grouping){
        if (comId == null){
            log.info("获取账号类型信息comId为空");
            return ResultUtil.error(ResultEnum.Err_203,"获取账号类型信息comId为空");
        }
        return ResultUtil.success(xfAccountTypeService.GetXFAccountType(comId,grouping));
    }

    // 新增账号类型信息(addXFAccountType)
    @PostMapping(PayConfigUrl.Url_AddXFAccountType)
    public Result AddXFAccountType(@Validated @RequestBody EntityXfAccountType entityXfAccountType){
        log.info("添加账户类型  参数【{}】",entityXfAccountType);
        if (entityXfAccountType.getComId() == null){
            log.info("新增账号类型信息comId为空");
            return ResultUtil.error(ResultEnum.Err_203,"新增账号类型信息comId为空");
        }
        if (entityXfAccountType.getAccTypeName() == null || entityXfAccountType.getAccTypeName().isEmpty()){
            log.info("新增账号类型信息 accTypeName 为空");
            return ResultUtil.error(ResultEnum.Err_203,"新增账号类型信息 accTypeName 为空");
        }
        try {
            //查询账号类型的编号和名称是否重复
            List<EntityXfAccountType> accountType=xfAccountTypeService.selectAccTypeName(entityXfAccountType.getComId(),entityXfAccountType);
            if (accountType !=null && accountType.size()>0){
                return ResultUtil.error(ResultEnum.Err_4031);
            }
            int i=xfAccountTypeService.AddXFAccountType(entityXfAccountType.getComId(),entityXfAccountType);
            if (i<=0){
                return ResultUtil.error(ResultEnum.Err_2016);
            }
        }catch (Exception exception){
            log.info("新增账号类型信息失败 exception{{}} ",exception.getMessage());
            return ResultUtil.error(ResultEnum.Err_813);
        }
        return ResultUtil.success();
    }

    // 修改账号类型信息(updateXFAccountType)
    @PostMapping(PayConfigUrl.Url_UpdateXFAccountType)
    public Result UpdateXFAccountType(@Validated @RequestBody EntityXfAccountType entityXfAccountType){
        if (entityXfAccountType.getComId() == null){
            log.info("修改账号类型信息comId为空");
            return ResultUtil.error(ResultEnum.Err_203,"修改账号类型信息comId为空");
        }
        if (entityXfAccountType.getAccTypeName() == null || entityXfAccountType.getAccTypeName().isEmpty()){
            log.info("修改账号类型信息 accTypeName 为空");
            return ResultUtil.error(ResultEnum.Err_203,"修改账号类型信息 accTypeName 为空");
        }
        if (entityXfAccountType.getAccTypeId() == null ){
            log.info("修改账号类型信息 accTypeId 为空");
            return ResultUtil.error(ResultEnum.Err_203,"修改账号类型信息 accTypeId 为空");
        }
        try {
            //查询账号类型的名称是否重复
            EntityXfAccountType accountType=xfAccountTypeService.selectAccTypeNameNull(entityXfAccountType.getComId(),entityXfAccountType);
            if (accountType !=null){
                return ResultUtil.error(ResultEnum.Err_4031);
            }
           int i=xfAccountTypeService.UpdateXFAccountType(entityXfAccountType.getComId(),entityXfAccountType);
            if (i<=0){
                return ResultUtil.error(ResultEnum.Err_3014);
            }
        }catch (Exception exception){
            log.info("修改账号类型信息失败 exception{{}} ",exception.getMessage());
            return ResultUtil.error(ResultEnum.Err_811);
        }
        redisUtil.dels("gbf#comId:"+entityXfAccountType.getComId());
        return ResultUtil.success();
    }

    // 获取账号类型明细信息(getXFAccountTypeDetails)
    @GetMapping(PayConfigUrl.Url_GetXFAccountTypeDetails)
    public Result<EntityXfAccountType> GetXFAccountTypeDetails(Long comId,Integer accTypeId){
        if (comId == null){
            log.info("获取账号类型明细信息comId为空");
            return ResultUtil.error(ResultEnum.Err_203,"获取账号类型明细信息comId为空");
        }
        if (accTypeId == null ){
            log.info("获取账号类型明细信息 accTypeId 为空");
            return ResultUtil.error(ResultEnum.Err_203,"获取账号类型明细信息 accTypeId 为空");
        }
        return ResultUtil.success(xfAccountTypeService.GetXFAccountTypeDetails(comId,accTypeId));
    }
}

